{% extends 'reference.html' %}

{% block title %}Grammar{% endblock %}

{% set docsNavBack={
    "url": "/docs/home.html",
    "text": "Kotlin Docs"
} %}

{% block page_content %}
    <div id="js-toc"></div>

    <div class="grammar page-content">
        <h1 id="grammar">Grammar</h1>

        {% for set in kotlinGrammar %}
            <div class="grammar-items-set" data-file-name="{{ set['file-name'] }}">
                {% for item in set.content %}
                    {% if item.type == 'comment' %}
                        <div class="grammar-comment">
                            {{ item.content | safe }}
                        </div>
                    {% elif item.type == 'item' %}
                        <div class="grammar-item">
                            {% for item in item.content %}
                                {% if item.type  == 'annotation' %}
                                    <div class="grammar-annotation">{{ item.content | safe }}</div>
                                {% elif item.type == 'declaration' %}
                                    <div class="grammar-declaration">
                                        <div class="grammar-declaration-name" id="{{ item.name }}">{{ item.name }}</div>

                                        {% if item.usages|length > 0 %}
                                            <div class="grammar-declaration-usedby">
                                                (used by
                                                {% for usage in item.usages %}
                                                    <a href="#{{ usage.name }}">{{ usage.name }}</a>
                                                    {%- if not loop.last -%}, {% endif %}
                                                {% endfor %})
                                            </div>
                                        {% endif %}
                                    </div>
                                {% elif item.type == 'description' %}
                                    {% if item.content|length > 0 %}
                                        <div class="grammar-description">
                                            {% for item in item.content %}
                                                {%- if item.type == 'identifier' -%}
                                                    <a class="grammar-identifier-name"
                                                       href="#{{ item.name }}">{{ item.name }}</a>
                                                {%- elif item.type == 'string' -%}
                                                    <span class="grammar-string">{{ item.content | safe }}</span>
                                                {%- elif item.type == 'symbol' -%}
                                                    <span class="grammar-symbol">{{ item.content | safe }}</span>
                                                {%- elif item.type == 'other' -%}
                                                    <span class="grammar-other">{{ item.content | safe }}</span>
                                                {%- elif item.type == 'whitespace' -%}
                                                    {{ item.content.replace(' ','&nbsp;') | safe }}
                                                {%- elif item.type == 'crlf' -%}
                                                    <br>
                                                {% endif %}
                                            {% endfor %}
                                        </div>
                                    {% endif %}
                                {% endif %}
                            {% endfor %}
                        </div>
                    {% endif %}
                {% endfor %}
            </div>
        {% endfor %}
    </div>
{% endblock %}

{% block scripts %}
    <script src="{{ url_for('static', filename='grammar.js')|autoversion }}"></script>
{% endblock %}
